package com.zzh.myapplication.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;

import net.icarplus.car.R;
import net.icarplus.car.constant.UrlValues;
import net.icarplus.car.manager.BaseActivity;
import net.icarplus.car.popuwindpw.ShowPopuWindow;
import net.icarplus.car.service.PublicRequest;
import net.icarplus.car.tools.LoadingUtil;
import net.icarplus.car.tools.S;
import net.icarplus.car.tools.SPUtils;
import net.icarplus.car.tools.T;
import net.icarplus.car.tools.ToolBarHelper;
import net.icarplus.car.tools.UserUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by Administrator on 2016/3/28.
 */
public class DayRenCarListDetailActicy extends BaseActivity {
    public Toolbar toolbar;
    private SimpleDraweeView simpleDraweeView;
    private Intent intent;
    private Button btn_submit;
    private String get_timeString = "";
    private String retrun_timeString = "";
    private TextView takeCarTimeTxt;
    private String userPhone;
    private LoadingUtil loadingUtil;
    public TextView day_car_num;
    public TextView retrun_car_time;
    ShowPopuWindow showPopuWindow = new ShowPopuWindow();
    public String appno;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1000:
                    DayRenCarListDetailActicy.this.loadingUtil.stopShowLoading();
                    Log.e("1000", msg.obj.toString());
//                    refreshCarList(msg.obj.toString());
                    //GetSubOrderResponse(msg.obj.toString());
                    Intent intent = new Intent(DayRenCarListDetailActicy.this, OrderPayCenterActivity.class);
                    intent.putExtra("appno", appno);
                    start_Activity(intent);

                    break;

                case 2000:
                    DayRenCarListDetailActicy.this.loadingUtil.stopShowLoading();
//                    if (msg.obj.toString() != null) {
//                        Gson gson = new Gson();
//                        ExpenseBean result = gson.fromJson(msg.obj.toString(),
//                                new TypeToken<ExpenseBean>() {
//                                }.getType());
//                        OrderBillingDetailBean orderBillingDetailBean = result.orderBillingDetail;
//                        //里程费
//                        mileagePrice = orderBillingDetailBean.priceUnitDesc;
//                        List<DepositChargeItemBean> depositChargeItemBeans = new ArrayList<DepositChargeItemBean>();
//                        List<AttachChargeItemBean> attachChargeItemBeans = new ArrayList<AttachChargeItemBean>();
//
//                        depositChargeItemBeans = orderBillingDetailBean.depositChargeItems;
//                        attachChargeItemBeans = orderBillingDetailBean.attachChargeItems;
//
//
//                    }

                    break;
                case 3000:
                    Log.e("3000", "3000000");
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    public void setContent() {
        setContentView(R.layout.activity_day_time_car_list_detail);
        userPhone = SPUtils.getString(this, "LoginInfo", null);
        boolean isLogin = SPUtils.getBoolean(this, "isLogin", false);
        if (!isLogin || S.isEmpty(userPhone)) {
            UserUtils.cleanUserInfo(this);
            T.showShort("请先登录！");
            Intent intent = new Intent(DayRenCarListDetailActicy.this, LoginActivity.class);
            start_Activity(intent);
            return;
        }
    }

    @Override
    public void initComponent() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        takeCarTimeTxt = (TextView) findViewById(R.id.tv_get_car_time);
        simpleDraweeView = (SimpleDraweeView) findViewById(R.id.iv_car_name);
        btn_submit = (Button) findViewById(R.id.btn_submit);
        intent = getIntent();
        String title = intent.getStringExtra("title");
        simpleDraweeView.setImageURI(Uri.parse(intent.getStringExtra("picUrl")));
        ToolBarHelper.setNormalTitleAndBack(DayRenCarListDetailActicy.this, toolbar, title);
        loadingUtil = new LoadingUtil(this, getString(R.string.loading_text));
        day_car_num = (TextView) findViewById(R.id.day_car_num);
        retrun_car_time = (TextView) findViewById(R.id.retrun_car_time);
        day_car_num.setText(intent.getStringExtra("carNum"));
    }

    @Override
    public void setListener() {
        btn_submit.setOnClickListener(this);
        takeCarTimeTxt.setOnClickListener(this);
        retrun_car_time.setOnClickListener(this);
    }

    @Override
    public void onAfterInit() {
    }

    public void SetDayCatListParams() {
        JSONObject object = new JSONObject();
        JSONObject listJson = new JSONObject();

        JSONArray array = new JSONArray();

        try {
            listJson.put("chargeItemCode", "0");
            listJson.put("itemName", "");
//          listJson.put("needFlag", "");
            listJson.put("chosenFlag", "0");
            array.put(listJson);

            object.put("mobile", userPhone);
            object.put("licenseNo", getIntent().getStringExtra("carNum"));
            object.put("orderMode", "01");
            object.put("rentType", "02");
            object.put("isBusiness", "0");
            object.put("userReason", "");
            object.put("qcRtNo", "");
            object.put("hcRtNo", "");
            object.put("isBusiness", "1");
            object.put("pttrcTime", get_timeString);
            object.put("prtrcTime", retrun_timeString);
            object.put("prcChargeDetList", array);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        PublicRequest.customMainRequest(this, UrlValues.SUBMIT_RENT_ORDER_URL, handler, object);

    }

    public void GetSubOrderResponse(String date) {
        try {
            JSONObject jsonObject = new JSONObject(date.toString());
            appno = jsonObject.getString("appNo");

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_submit:
                this.loadingUtil.startShowLoading();
                SetDayCatListParams();
                break;
            case R.id.tv_get_car_time:
                showPopuWindow.showTimePop(this, v, 0, new ShowPopuWindow.onSelectTimeString() {
                    @Override
                    public void onTimeString(String timeString) {
                        get_timeString = timeString;
                        takeCarTimeTxt.setText(get_timeString);
                    }
                });
                break;
            case R.id.retrun_car_time:
                showPopuWindow.showTimePop(this, v, 0, new ShowPopuWindow.onSelectTimeString() {
                    @Override
                    public void onTimeString(String timeString) {
                        retrun_timeString = timeString;
                        retrun_car_time.setText(retrun_timeString);
                    }
                });
                break;
        }
    }
}
